﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;


/// <summary>
/// Summary description for SeriesLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class SeriesLinqProvider : ISeriesProvider
    {
        public SeriesLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public List<Series> getOnGoingSeriesByCountry(int cID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Match> _matchList = new List<Match>();
                List<Series> _SeriesList = new List<Series>();

                var series = from aSeries in dc.Series
                             where ((aSeries.Country1ID == cID) || (aSeries.Country2ID == cID))
                             select aSeries;
                foreach (var s in series)
                {
                    Series _ser = extractSeries(s);
                    if (MatchService.Instance.getOnGoingSeries(_ser.SeriesID))
                        _SeriesList.Add(_ser);
                }
                return _SeriesList;
            }
        }


        public List<Series> getUpComingSeriesByCountry(int cID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Match> _matchList = new List<Match>();
                List<Series> _SeriesList = new List<Series>();

                var series = from aSeries in dc.Series
                             where ((aSeries.Country1ID == cID) || (aSeries.Country2ID == cID))
                             select aSeries;
                foreach (var s in series)
                {
                    Series _ser = extractSeries(s);
                    if (MatchService.Instance.getUpComingSeries(_ser.SeriesID))
                        _SeriesList.Add(_ser);
                }
                return _SeriesList;
            }
        }

        public List<Series> getCompletedByCountry(int cID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Match> _matchList = new List<Match>();
                List<Series> _SeriesList = new List<Series>();

                var series = from aSeries in dc.Series
                             where ((aSeries.Country1ID == cID) || (aSeries.Country2ID == cID))
                             select aSeries;
                foreach (var s in series)
                {
                    Series _ser = extractSeries(s);
                    if (MatchService.Instance.getCompletedSeries(_ser.SeriesID))
                        _SeriesList.Add(_ser);
                }
                return _SeriesList;
            }
        }        public List<Series> getOnGoingSeries()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Series> _SeriesList = new List<Series>();

                var series = from aSeries in dc.Series
                             select aSeries;

                foreach (var s in series)
                {
                    Series _ser = extractSeries(s);
                    if (MatchService.Instance.getOnGoingSeries(_ser.SeriesID))
                        _SeriesList.Add(_ser);
                }
                return _SeriesList;
            }
        }

        public List<Series> getUpComingSeries()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Series> _SeriesList = new List<Series>();

                var series = from aSeries in dc.Series
                             select aSeries;

                foreach (var s in series)
                {
                    Series _ser = extractSeries(s);
                    if (MatchService.Instance.getUpComingSeries(_ser.SeriesID))
                        _SeriesList.Add(_ser);
                }
                return _SeriesList;
            }
        }

        public Series getSeries(int seriesID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var series = from aSeries in dc.Series
                             where aSeries.SeriesID == seriesID
                             select aSeries;

                if (series == null)
                {
                    return null;
                }

                if (series.Count<Entity.Series>() == 1)
                {
                    return extractSeries(series);
                }
                throw new Exception("Invalid Series Id " + seriesID + " !");
            }
        }

        private Series extractSeries(IQueryable<Entity.Series> series)
        {
            Series aSeries = new Series(
                series.Single<Entity.Series>().SeriesID,
                Convert.ToInt16(series.Single<Entity.Series>().Country1ID),
                Convert.ToInt16(series.Single<Entity.Series>().Country2ID),
                series.Single<Entity.Series>().SeriesName);

            return aSeries;
        }

        private Series extractSeries(Entity.Series s)
        {
            Series aSeries = new Series(
                s.SeriesID,
                Convert.ToInt16(s.Country1ID),
                Convert.ToInt16(s.Country2ID),
                s.SeriesName);
            return aSeries;
        }
    }
}